<template>
    <TableForm
        ref="Table"
        :renderData="renderData"
        :list="list || formData"
        :height="height"
        :loading="loading"
        :hideAdd="hideAdd"
        :hideDel="hideDel"
        :beforeAdd="beforeAdd"
        :beforeDel="beforeDel"
        :summary="summary"
    />
</template>

<script>
import CellInput from "@/components/CellInput";
import getRenderData from "./tableFormData";
import TableForm from "./index.vue";
export default {
    components: {
        CellInput,
        TableForm,
    },
    props: [
        "column",
        "list",
        "height",
        "loading",
        "hideAdd",
        "hideDel",
        "beforeAdd",
        "beforeDel",
        "summary",
    ],
    data: () => {
        return {
            renderData: [],
            formData: [],
            table: "",
        };
    },
    computed: {},
    watch: {
        
    },
    created() {
        let { column } = this;
        let { formData, rendetData } = getRenderData(column, this);
        this.formData = formData;
        this.renderData = rendetData;
    },
    mounted() {
        this.table = this.$refs.table;
    },
    methods: {
        // 金额计算 ： 单价 * 码单
        moneyCount(d) {
            let { noteCount, price } = d;
            let t = (noteCount || 0) * (price || 0);
            t = t.toFixed(3);
            d.money = t || 0;
            this.checkSync("money", d);
        },
        // 单包方数计算 ：厚度/1000*宽度/1000*长度*片数*件数
        checkOneCount(d) {
            let { ply, width, length, sliceCount, pieceCount } = d;
            if (!ply || !width || !length || !sliceCount) return false;
            let t = (ply * width * length * sliceCount) / 1000000;
            d.oneCount = t;
            !d.oneNoteCount && (d.oneNoteCount = t);
            this.checkSync("oneCount", d);
            return t;
        },
        // 码单计算  单包码单* 件数
        noteCount(d) {
            let { oneNoteCount, pieceCount } = d;
            let t = (oneNoteCount || 0) * (pieceCount || 0);
            t = t.toFixed(3);
            d.noteCount = t;
            this.checkSync("noteCount", d);
        },
        // 方数计算 单包方数*件数
        inCount(d) {
            let { oneCount, pieceCount } = d;
            !oneCount && (oneCount = this.checkOneCount(d));
            let t = ((oneCount || 0) * (pieceCount || 0)).toFixed(3);
            d.inCount = t;
            !Number(d.noteCount) && (d.noteCount = t);
            this.checkSync("inCount", d);
        },
        checkForm(cb) {
            this.$refs.Table.checkForm(cb);
        },
        /**
         * 同步计算的值
         * 当某个字段需要用到inCount函数的计算来获取结果时
         * 且该字段名称部位inCount时 通过checkSync来同步这个结果
         * 例：  count 、 xxxCount需要通过inCount()计算时用到
         *      具体参考 allocating.vue 页面配置
         */
        checkSync(k, obj) {
            let { column } = this;
            column.find((v) => {
                let { value, sync } = v;
                if (sync && sync === k) {
                    obj[value] = obj[sync];
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
</style>